﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace StudentInfo
{
    class Student
    {

        string firstName { set; get; }
        string lastName;
        int fN;
        long tel;
        string email;
        List<int> marks;
        int groupNumber;
        #region Properties
        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        public int FN
        {
            get { return fN; }
            set { fN = value; }
        }

        public long Tel
        {
            get { return tel; }
            set { tel = value; }
        }

        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        public List<int> Marks
        {
            get { return marks; }
            set { marks = value; }
        }

        public int GroupNumber
        {
           get {return groupNumber;}
           set {groupNumber=value;}
        }

       
        #endregion

        public Student(string FirstName, string LastName, int FN, long Tel, string Email, List<int> Marks, int GroupNumber)
        {
            this.firstName = FirstName;
            this.lastName = LastName;
            this.fN = FN;
            this.tel = Tel;
            this.email = Email;
            this.marks = Marks;
            this.groupNumber = GroupNumber;

        }

        
        //Check whether some number is specific times in the list of marks
        public bool Check(int number){
                 if (marks.Contains(number))
	            {
                    int length = marks.Count;
                    while (marks.Contains(number))
                    {
                        marks.Remove(number);
                    }
                    int newlength = marks.Count;

                    for (int i = 1; i <= length - marks.Count; i++)
                    {
                        marks.Add(number);

                    }
                    if (length==newlength+2)
                    {
                                                
                        return true;
                    }
                    else
                    {
                        return false;
                    }
	            }
                 else
                 {
                     return false;
                 }
        }
       

        static void Main(string[] args)
        {
            //Student 1
            List<int> MarkStudent1 = new List<int>{3,2,2,5,6,4};
            Student student1=new Student("Ivan","Gerganov",613609,359888444125,"vanko@abv.bg",MarkStudent1,2);

            //Student 2
            List<int> MarkStudent2 = new List<int> { 3, 2, 2, 6,5,4 };
            Student student2 =  new Student("Georgi","Ivanov",625908,359888636815,"Gosho@abv.bg",MarkStudent2,1 );

            //Student 3
            List<int> MarkStudent3 = new List<int> { 6, 6, 5, 6 ,3,5};
            Student student3 = new Student("Albert","Smith",856206,10002893256123,"rapara@gmail.com",MarkStudent3,2);

            //Student 3
            List<int> MarkStudent4 = new List<int> { 3, 4, 5, 6,2,2,2 };
            Student student4 = new Student("Anna", "Kornikova", 651810,3592682593, "Ani@gmail.com", MarkStudent3, 1);

            //Adding the students in the list
            List<Student> students = new List<Student>() {student1,student2,student3,student4};

            //Get the students from Group 2 and sort the alphabetically
            var subset = from g in students
                         where g.groupNumber==2
                         orderby g.firstName
                         select g;

            //Task 2-Extention method
            var sub = students.Select(s => s).Where(s => s.groupNumber == 2).OrderBy(s => s.firstName);

            Console.WriteLine("The students from Group Number 2 are:");
            foreach (Student item in sub)
            {
                Console.WriteLine(item.firstName);
            }

            Console.WriteLine();
            Console.WriteLine("The students from Group Number 2 are:");
            foreach (Student item in subset)
            {
                Console.WriteLine(item.firstName);
            }

            Console.WriteLine();
           

            //Task 3 - extract the students with email abv.bg through LINQ
            var mailFilter = from mail in students
                             where mail.email.EndsWith("abv.bg")
                             select mail;

            Student[] emails=new Student[10];
            int i =0;
            foreach (Student item in students)
            {
                if (item.email.EndsWith("abv.bg"))
                {
                    emails[i] = item;
                }
            }

            Console.WriteLine("The emails in abv.bg are:");
            foreach (Student st in mailFilter)
            {
               
                Console.WriteLine(st.email);
            }

            //Regular expression
            Regex rgx = new Regex(@"3592(\d)*");

            //Extract the students with phones in Sofia
            var phoneSofia = from phone in students
                             where rgx.IsMatch(phone.tel.ToString())
                             select phone;

            Console.WriteLine();
            Console.WriteLine("The telephones from Sofia are:");
            foreach (var item in phoneSofia)
            {
                Console.WriteLine(item.tel);
            }

            Console.WriteLine();

            var marks = from mark in students
                        where mark.marks.Contains(6)
                        select mark;
            
            foreach (var item in marks)
            {

                var  studentName= new{ FullName = item.firstName + " " + item.lastName, Marks = item.marks };
                Console.Write(studentName.FullName+" has:");
                foreach (var mark in studentName.Marks)
                {
                    Console.Write(mark+" ");
                }
                Console.WriteLine();
     
            }
            Console.WriteLine();

            //The students with extactly 2 poor marks
            var studentsWithPoor = students.Select(s=>s).Where(s=>s.Check(2));

            foreach (Student poorStudent in studentsWithPoor)
            {
                
                Console.WriteLine(poorStudent.firstName+" "+poorStudent.lastName+" has two marks \"2\"");
            }

            Regex reg =  new Regex(@"\d{4}06");
            //The marks of the students that enrolled in 2006 
            var marks2006 = from s in students
                            where reg.IsMatch(s.fN.ToString())
                            select s.marks;
           
            Console.WriteLine("\nThe marks of the students who enrolled ine 2006 are:");
            foreach (var item in marks2006)
            {
                foreach (var ite1 in item)
                {
                    Console.Write("{0} ",ite1);
                }
            }
            Console.WriteLine();

            Group group1 = new Group();
            group1.DepartmentName = "Physic";
            group1.GroupNumber = 1;

            Group group2 = new Group();
            group2.DepartmentName = "Mathematics";
            group2.GroupNumber = 2;

            List<Group> groups = new List<Group>{group2,group1};

            
            student1.groupNumber = group2.GroupNumber;


            //The students who are in the mathematics department
            var mathStudents = from s in students
                               join g in groups on s.groupNumber equals g.GroupNumber
                               where g.GroupNumber==2
                               select new { Student = s, DepartmentName = g.DepartmentName };

            foreach (var st in mathStudents)
            {
                Console.WriteLine("{0} is in the {1}"+ 
                    "Department",st.Student.Email,st.DepartmentName);
            }
            Console.WriteLine();


        }
    }
}
